package com.ea.modular.controller.sys;


import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.quartz.CronExpression;
import org.quartz.SchedulerException;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.ea.common.annotation.LogAnnotation;
import com.ea.common.parent.controller.BaseController;
import com.ea.common.utils.DateUtils;

import io.swagger.annotations.ApiOperation;

import com.ea.modular.service.sys.JobService;
import com.ea.modular.model.sys.Job;


/**
 * Controller控制器
 * @author jun
 *
 */
@Controller
@RequestMapping(value="/job")
public class JobController extends BaseController {

    @Autowired
    private JobService service;

    private String PREFIX = "/system/job/";
    
    @Autowired
	private ApplicationContext applicationContext;

	/**
	 * 跳转到主页
	 * @return
	 */
	@ApiOperation(value = "首页")
	@RequestMapping(value = "",method=RequestMethod.GET)
	public String home() {		 
		return PREFIX + "job";
	}

	/**
	 * 列表
	 * @param Job
	 * @return
	 */
	@LogAnnotation
	@ApiOperation(value = "列表")
	@RequestMapping(value = "/list",method=RequestMethod.POST)
	@ResponseBody
	public Object list(Job job){
		return service.findPage(job);
	}
	
	/**
	 * 删除
	 * @return
	 * @throws SchedulerException 
	 */
	@LogAnnotation
	@ApiOperation(value = "删除")
	@RequestMapping(value = "/delete",method=RequestMethod.POST)
	@ResponseBody
	public int delete(String id,String jobName) throws SchedulerException {
		return service.deleteJob(id,jobName);
	}
	
	
     /**
	 * 详情页
	 * @return
	 */
	@ApiOperation(value = "详情")
	@RequestMapping(value = "/form",method=RequestMethod.GET)
	public String form(Model model,Job job,String type) {

		//判断是否为修改页
		if(type.equals("update")) {
			Job u = service.get(job);
		
			model.addAttribute("job",u);
			//跳转修改页
			return PREFIX + "job_edit";
		}

		//跳转添加页
		return PREFIX + "job_add";
	}

	/**
	 * 管理
	 * @return
	 */
	@LogAnnotation
	@ApiOperation(value = "编辑")
	@RequestMapping(value = "/save",method=RequestMethod.POST)
	@ResponseBody
	public int save(Job job,String type) {
		return service.saveJob(job,type);
	}
	
	/**
	 * 查询所有的类名
	 * @return
	 */
	@ApiOperation(value = "springBean名字")
	@RequestMapping(value = "/beans",method=RequestMethod.POST)
	@ResponseBody
	public List<String> listAllBeanName() {
		String[] strings = applicationContext.getBeanDefinitionNames();
		List<String> list = new ArrayList<>();
		for (String str : strings) {
			if (str.contains(".")||!str.contains("Controller")) {
				continue;
			}

			Class<?> clazz = getClass(str);
			if (clazz.isAssignableFrom(Controller.class) || clazz.isAnnotationPresent(RestController.class)) {
				continue;
			}

			list.add(str);
		}
		list.sort((l1, l2) -> l1.compareTo(l2));

		return list;
	}
	/**
	 * 根据类名查询所有的方法名
	 * @return
	 */
	@ApiOperation(value = "springBean的无参方法")
	@RequestMapping(value = "/beans/{name}",method=RequestMethod.POST)
	@ResponseBody
	public Set<String> listMethodName(@PathVariable String name) {
		Class<?> clazz = getClass(name);
		Method[] methods = clazz.getDeclaredMethods();

		Set<String> names = new HashSet<>();
		Arrays.asList(methods).parallelStream().forEach(m -> {
			int b = m.getModifiers();// public 1 static 8 final 16
			if (b == 1 || b == 9 || b == 17 || b == 25) {
				Class<?>[] classes = m.getParameterTypes();
				if (classes.length == 0) {
					names.add(m.getName());
				}
			}
		});

		return names;
	}
	private Class<?> getClass(String name) {
		Object object = applicationContext.getBean(name);
		Class<?> clazz = object.getClass();
		if (AopUtils.isAopProxy(object)) {
			clazz = clazz.getSuperclass();
		}

		return clazz;
	}
	
	/**
	 * 校验cron表达式
	 * @return
	 */
	@ApiOperation(value = "校验cron表达式")
	@RequestMapping(value = "/cron",method=RequestMethod.POST)
	@ResponseBody
	public boolean checkCron(String cron) {
		return CronExpression.isValidExpression(cron);
	}
	
	
	/**
	 * 开始任务
	 * @return
	 * @throws SchedulerException 
	 */
	@LogAnnotation
	@ApiOperation(value = "开始任务")
	@RequestMapping(value = "/start",method=RequestMethod.POST)
	@ResponseBody
	public int start(String id,String jobName) throws SchedulerException {
		return service.start(id,jobName);
	}
	
	/**
	 * 关闭任务
	 * @return
	 * @throws SchedulerException 
	 */
	@LogAnnotation
	@ApiOperation(value = "关闭任务")
	@RequestMapping(value = "/end",method=RequestMethod.POST)
	@ResponseBody
	public int end(String id,String jobName) throws SchedulerException {
		return service.end(id,jobName);
	}
	
	
	
	 /**
     * 测试定时任务
     */
	@ApiOperation(value = "测试任务")
    @RequestMapping(value = "/text1")
	@ResponseBody
    public Object outtree() {
       System.out.println("当前时间:"+DateUtils.getDateTime()+"111111111111111");
       return DateUtils.getDateTime();
	}
    
	 /**
     * 测试定时任务
     */
	@ApiOperation(value = "测试任务2")
    @RequestMapping(value = "/text2")
	@ResponseBody
    public Object outtree2() {
       System.out.println("当前时间:"+DateUtils.getDateTime()+"22222222222222222");
       return DateUtils.getDateTime();
	}
}
